package com.newasia.management.controller;/**
 * Created by mic on 2019/8/1.
 */

import com.newasia.commons.result.ResponseData;
import com.newasia.commons.result.ResponseUtil;
import com.newasia.management.IManagementItemsService;
import com.newasia.management.constants.ManagementRetCode;
import com.newasia.management.dto.productSku.*;
import com.newasia.management.dto.productSkuPerm.ChangeSkuPermRequest;
import com.newasia.management.dto.productSkuPerm.ChangeSkuPermResponse;
import com.newasia.management.dto.productSpec.*;
import com.newasia.management.dto.productSpecValue.*;
import com.newasia.management.dto.productSpu.*;
//import com.newasia.search.InitDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 腾讯课堂搜索【咕泡学院】
 * 官网：www.gupaoedu.com
 * 风骚的Mic 老师
 * create-date: 2019/8/1-下午5:27
 */
@Slf4j
@RestController
@RequestMapping("/management")
public class ItemController {

    @Reference(timeout = 3000)
    IManagementItemsService managementItemsService;



//    @Reference(timeout = 3000)
//    private InitDataService initDataService;



    /**商品**/
    /**增--父*/
    @PostMapping("/spu")
    public ResponseData createSpu(@RequestBody CreateSpuRequest request, HttpServletRequest httpServletRequest){

        CreateSpuResponse response= managementItemsService.createSpu(request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }


    @PostMapping("/sku")
    public ResponseData createSku(@RequestBody CreateSkuRequest request, HttpServletRequest httpServletRequest){

        CreateSkuResponse response= managementItemsService.createSku(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }


    /**删-父*/
    @DeleteMapping("/spu/{spuId}")
    public ResponseData deleteSpu(@PathVariable("spuId") Long spuId, HttpServletRequest httpServletRequest){

        DeleteSpuResponse response= managementItemsService.deleteSpu(spuId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**删-子*/
    @DeleteMapping("/sku/{skuId}")
    public ResponseData deleteSku(@PathVariable("skuId") Long skuId, HttpServletRequest httpServletRequest){

        DeleteSkuResponse response= managementItemsService.deleteSku(skuId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


    /**改-父*/
    @PutMapping("/spu/{spuId}")
    public ResponseData changeSpu(@PathVariable("spuId") Long spuId, @RequestBody ChangeSpuRequest request, HttpServletRequest httpServletRequest){

        ChangeSpuResponse response= managementItemsService.changeSpu(spuId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }

    /**改-子*/
    @PutMapping("/sku/{skuId}")
    public ResponseData changeSku(@PathVariable("skuId") Long skuId, @RequestBody ChangeSkuRequest request, HttpServletRequest httpServletRequest){

        ChangeSkuResponse response= managementItemsService.changeSku(skuId,request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }


    /**改-子-代理商价格*/
    @PutMapping("/skuPerm")
    public ResponseData changeSkuPerm(@RequestBody ChangeSkuPermRequest request, HttpServletRequest httpServletRequest){

        ChangeSkuPermResponse response= managementItemsService.changeSkuPerm(request);


        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
//            initDataService.deleteItem();
//            initDataService.initItems();
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);

    }



    /**查--父--多个*/
    @GetMapping("/spu")
    public ResponseData getSupList(GetGoodListRequest request, HttpServletRequest httpServletRequest){

        GetGoodListResponse response = managementItemsService.getGoodList(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**查--父--一个*/
    @GetMapping("/spuItem")
    public ResponseData getSupItem(@RequestParam Long spuId, HttpServletRequest httpServletRequest){
        GetGoodResponse response = managementItemsService.getGood(spuId);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**查--子*/
    @GetMapping("/sku")
    public ResponseData getSkuList(GetSkuListRequest request, HttpServletRequest httpServletRequest){



        GetSkuListResponse response = managementItemsService.getSkuList(request);

        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }

    /**查--子*/
    @PutMapping("/batchShelves")
    public ResponseData updateShelvesSpuList(@RequestBody ShelvesSpuListRequest request){
        ShelvesSpuListResponse response = managementItemsService.updateShelvesSpuList(request);
        if(response.getCode().equals(ManagementRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(response);
        }
        return  new ResponseUtil().setErrorMsg(response);
    }


}
